<?php

namespace App\Console\Commands;

use App\Library\Logic\AuctionBailLogic;
use App\Library\Logic\AuctionLogic;
use App\Library\Logic\AuctionOrderLogic;
use Exception;
use Illuminate\Console\Command;

class AuctionBailRefundAllCommand extends Command
{
    /**
     * 退拍卖商定保释金金额
     *
     * @var string
     */
    protected $signature = 'command:AuctionBailRefundAllCommand {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     * @throws Exception
     */
    public function handle(): void
    {
        $serialNumber = $this->argument('serialNumber');
        if (empty($serialNumber) || strlen($serialNumber) !== 20) return;
        $auction = AuctionLogic::findBySerialNumber($serialNumber);
        if (is_null($auction)) return;
        else if ($auction->is_end === 0) return;
        else if ($auction->expired !== 1 && $auction->sell_out !== 1) return;
        $userId = (int)AuctionOrderLogic::valueByWhere('auctionId', $auction->id, 'userId');
        AuctionBailLogic::auctionBailRefund($auction, $userId);
    }
}
